package com.lin.learning.questionmanagement.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.lin.learning.questionmanagement.mapper.AdminMapper;
import com.lin.learning.questionmanagement.mapper.TeacherMapper;
import com.lin.learning.questionmanagement.mapper.TeacherVoMapper;
import com.lin.learning.questionmanagement.service.AdminService;
import com.lin.learning.questionmanagement.service.LoginService;
import com.lin.learning.questionmanagement.service.TeacherService;
import com.lin.learning.questionmanagement.service.TeacherVoService;
import com.lin.learning.questionmanagement.utils.JwtUtil;
import com.lin.learning.questionmanagement.utils.RedisCache;
import com.lin.learning.questionmanagement.vo.AdminVo;
import com.lin.learning.questionmanagement.vo.TeacherVo;
import com.lin.learning.questionmanagement.vo.UserVo;
import com.our.learning.infrastructure.po.Administrator;
import com.our.learning.infrastructure.po.Teacher;
import com.our.learning.infrastructure.po.User;
import com.our.learning.infrastructure.pojo.ResponseResult;
import com.studentmanagement.mapper.StudentMapper;
import com.studentmanagement.po.StudentPO;
import com.studentmanagement.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class LoginServiceImpl implements LoginService {

/*    @Autowired
    private RedisCache redisCache;*/

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherMapper teacherMapper;


    @Autowired
    private AdminMapper adminMapper;


    @Override
    public ResponseResult userLogin(User user) {
        UsernamePasswordAuthenticationToken authenticationToken=new UsernamePasswordAuthenticationToken(user.getAccount(),user.getPassword());
        Authentication authentication=authenticationManager.authenticate(authenticationToken);
        if (Objects.isNull(authentication)){
            return ResponseResult.errorResult(505,"用户名或密码错误");
        }
        User loginuser=(User) authentication.getPrincipal();
        loginuser.setType(user.getType());
        String id= loginuser.getId().toString();
        String jwt= JwtUtil.createJWT(id);
        //redisCache.setCacheObject("login:"+id,user);
        String name = null;
        Integer classId=null;
        if ("student".equals(loginuser.getType())){
            if (studentService.studentListLocal(loginuser.getId(),null).size()==0){
                return ResponseResult.errorResult(505,"用户名或密码错误");
            }
            List<StudentPO> list = studentService.studentListLocal(loginuser.getId(), null);
            name=list.get(0).getName();
            classId=list.get(0).getClassId();
        }else if("teacher".equals(loginuser.getType()))
        {
            if (teacherMapper.selectByTeacher_id(loginuser.getId())==null){
                return ResponseResult.errorResult(505,"用户名或密码错误");
            }
            TeacherVo teacher = teacherMapper.selectByTeacher_id(loginuser.getId());
            name=teacher.getName();
            classId=teacher.getClassId();
        }else {
            if (adminMapper.selectByAdmin_id(loginuser.getId())==null){
                return ResponseResult.errorResult(505,"用户名或密码错误");
            }
            AdminVo administrator = adminMapper.selectByAdmin_id(loginuser.getId());
            name=administrator.getName();
        }
        UserVo vo=new UserVo(id,loginuser.getAccount(),jwt,loginuser.getType(),name,classId);

        return ResponseResult.okResult(vo);
    }

    @Override
    public ResponseResult logout() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User loginUser=(User) authentication.getPrincipal();
        //获取id
        Integer id=loginUser.getId();
        //redisCache.deleteObject("login:"+id);
        return ResponseResult.okResult();
    }
}
